package com.spic.business.train.plan.domain;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.spic.business.train.trainInfo.dto.ClassDto;
import com.spic.business.train.trainInfo.dto.TrainCourseWareDTO;
import com.spic.business.train.trainInfo.dto.TrainInfoDto;
import com.spic.common.core.annotation.Excel;
import com.spic.common.core.web.domain.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 培训计划对象 train_plan
 * 
 * @author spic
 * @date 2021-10-15
 */
@ApiModel
@Data
public class TrainPlan extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** $column.columnComment */
    private String id;

    /** 年份 */
    @ApiModelProperty("计划年份")
    @NotNull(message = "计划年份不能为空！")
    @Excel(name = "计划年份")
    private String year;

    /** 培训类型（数据字典 train_type） */
    @NotBlank(message = "培训类型不能为空！")
    @Excel(name = "培训类型", dictType ="train_type")
    @ApiModelProperty("培训类型")
    @ExcelProperty({"南露天矿2022年安全培训计划", "培训类型"})
    private String trainType;

    /** 部门id */
    @NotNull(message = "申报部门不能为空！")
    @Excel(name = "部门",cahceType =Excel.CacheType.DEPT)
    @ApiModelProperty("申报部门部门")
    private Long deptId;

    /** 申报部门及其下属部门 */
    @TableField(exist = false)
    private List<Long> deptIds;

    @TableField(exist = false)
    @ApiModelProperty(value = "申报部门名称", notes = "仅仅展示用")
    @ExcelProperty({"南露天矿2022年安全培训计划", "申报部门名称"})
    private String deptName;

    /** 项目名称 */
    @NotBlank(message = "项目名称不能为空！")
    @Excel(name = "项目名称")
    @ApiModelProperty("项目名称")
    @ExcelProperty({"南露天矿2022年安全培训计划", "项目名称"})
    private String trainName;

    /** 培训方式（数据字典train_method） */
    @NotBlank(message = "培训方式不能为空！")
    @Excel(name = "培训方式",dictType = "train_method")
    @ApiModelProperty("培训方式")
    @ExcelProperty({"南露天矿2022年安全培训计划", "人员类别及参培人数", "领导班子"})
    private String trainMethod;

    /** 培训师 */
    @Excel(name = "培训师")
    @ApiModelProperty("培训师")
    @ExcelProperty({"南露天矿2022年安全培训计划", "人员类别及参培人数", "中层管理人员"})
    private String trainer;

    /** 培训师选取方式（数据字典choose_way） */
    @Excel(name = "培训师选取方式",dictType = "choose_way")
    @ApiModelProperty("培训师选取方式")
    @ExcelProperty({"南露天矿2022年安全培训计划", "人员类别及参培人数", "一般管理人员"})
    private String chooseWay;

    @Excel(name = "培训机构和地点")
    @ApiModelProperty("培训机构和地点")
    @ExcelProperty({"南露天矿2022年安全培训计划", "人员类别及参培人数", "员工","工队长"})
    private String trainPlace;

    /** 培训机构 */
    @Excel(name = "培训机构")
    @ApiModelProperty("培训机构")
    @ExcelProperty({"南露天矿2022年安全培训计划", "人员类别及参培人数", "员工","岗位员工"})
    private String trainMechanism;

    /** 天数 */
    @Excel(name = "天数")
    @ApiModelProperty("总计班天")
    private BigDecimal dayNumber;

    /** 总课时 计算：办班数 * 天数 * 课时/班 */
    @Excel(name = "总课时 计算：办班数 * 天数 * 课时/班")
    @ApiModelProperty("总课时")
    private String classHourNumber;

    /** 讲课酬金 */
    @Excel(name = "讲课酬金")
    @ApiModelProperty("讲课酬金")
    private BigDecimal lectureFee;

    /** 培训费 */
    @Excel(name = "培训费")
    @ApiModelProperty("培训费")
    private BigDecimal trainingFee;

    /** 差旅费 */
    @Excel(name = "差旅费")
    @ApiModelProperty("差旅费")
    private BigDecimal travelExpenses;

    /** 资料费 */
    @Excel(name = "资料费")
    @ApiModelProperty("资料费")
    private BigDecimal dataDee;

    /** 其他费用 */
    @Excel(name = "其他费用")
    @ApiModelProperty("其他费用")
    private BigDecimal otherFee;

    /** 总费用 */
    @Excel(name = "总费用")
    @ApiModelProperty("总费用")
    private BigDecimal totalCost;

    /** 考核方式（数据字典 assessment_method） */
    @Excel(name = "考核方式",dictType = "assessment_method")
    @ApiModelProperty("考核方式")
    private String assessmentMethod;

    /** 删除状态 0正常 1删除 */
    private String deleteFlag;

    /** 执行状态 0未执行 1执行中 2执行结束 */
    @Excel(name = "执行状态",dictType = "train_plan_status")
    @ApiModelProperty("执行状态")
    private String implementFlag;

    /** 应急预案台账id **/
    private Long planStandingBookId;

    @ApiModelProperty("课件id列表")
    private String trainCoursewareId;
    @ApiModelProperty("培训方案-关联课件信息")
    private List<TrainCourseWareDTO> trainCoursewareList;

    @ApiModelProperty("培训年度计划id")
    private String trainPlanYearId;

    @ApiModelProperty("培训内容")
    @Excel(name = "培训内容")
    private String trainContent;

    @ApiModelProperty("培训班数 人数")
    private Integer classNumber;

    @ApiModelProperty("参培人数总计")
    private Integer peopleNumber;

    @ApiModelProperty("培训对象")
    private String trainPersoneel;

    @ApiModelProperty("培训方案-培训班数信息")
    private List<TrainPlanShift> trainPlanShiftList;

    @ApiModelProperty("培训方案-培训班数")
    private List<TrainPlanStaff> trainPlanStaffs;

    public void checkParam(){
        if (CollectionUtils.isNotEmpty(trainPlanShiftList)) {
            trainPlanShiftList.forEach(TrainPlanShift::checkParam);
        }
        if (CollectionUtils.isNotEmpty(trainPlanStaffs)) {
            trainPlanStaffs.forEach(TrainPlanStaff::checkParam);
        }
    }

    /**
     * 转换为培训信息
     * @return
     */
    public TrainInfoDto toTrainInfoDto(){
        TrainInfoDto infoDto = new TrainInfoDto();
        infoDto.setPlanId(this.getId());

        if(CollectionUtils.isNotEmpty(this.getTrainCoursewareList())) {
            infoDto.setTrainCoursewareList(this.getTrainCoursewareList());
        }
        List<ClassDto> classDtos = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(trainPlanShiftList)) {
            for(TrainPlanShift shift: trainPlanShiftList ){
                ClassDto classDto = new ClassDto();
                classDto.setTrainPlanShiftId(shift.getId());
                String[] userIds = shift.getLearnerIds().split(",");
                List<Long> userIdsLongArray =Stream.of(userIds).map(item->Long.parseLong(item)).collect(Collectors.toList());
                classDto.setUserIds(userIdsLongArray);
                classDtos.add(classDto);
            }
        }
        infoDto.setclassDtos(classDtos);
        return infoDto;
    }
}
